package com.logicwin.aibala.services.file;

import java.util.Map;

import java.util.WeakHashMap;

import java.util.concurrent.ConcurrentHashMap;

import java.util.concurrent.locks.Lock;

import java.util.concurrent.locks.ReentrantLock;

import android.graphics.Bitmap;

public final class FileCache<K, V> {

	private final Lock lock = new ReentrantLock();

	private final int maxCapacity;

	private final Map<K, V> eden;

	private final Map<K, V> longterm;
	
	public static FileCache<String, Bitmap> mInstance = null;

	public FileCache(int maxCapacity) {

		this.maxCapacity = maxCapacity;
		this.eden = new ConcurrentHashMap<K, V>(maxCapacity);
		this.longterm = new WeakHashMap<K, V>(maxCapacity);

	}
	
	public static FileCache<String, Bitmap> Instance(){
		if (mInstance == null){
			mInstance = new FileCache<String, Bitmap>(200);
		}
		return mInstance;
	}

	public V get(K k) {
		
		V v = this.eden.get(k);
		if (v == null) {
			lock.lock();
			try {
				v = this.longterm.get(k);
			} finally {
				lock.unlock();
			}
			if (v != null) {
				this.eden.put(k, v);
			}
		}
		return v;
	}

	public void put(K k, V v) {

		if (this.eden.size() >= maxCapacity) {
			lock.lock();
			try {
				this.longterm.putAll(this.eden);
			} finally {
				lock.unlock();
			}
			this.eden.clear();
		}
		this.eden.put(k, v);
	}

}
